﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;
using System.IO;

public partial class Admin_Module_Photo_Controls_PhotoList : System.Web.UI.UserControl
{
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    private const int PageSize = 30;
    public Guid AlbumId { get; set; }
    private string GridViewSortDirection
    {

        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = "ASC";
            return ViewState["sortDirection"].ToString();
        }
        set { ViewState["sortDirection"] = value; }

    }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
                return int.Parse(Request["page"].ToString());
            return 1;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["AlbumId"] != null && !Request["AlbumId"].Equals(Guid.Empty.ToString()))
                AlbumId = new Guid(Request["AlbumId"]);
            else
                Response.Redirect("/Admin/Module/Album/AlbumList.aspx");
            BindData("CreatedDate DESC");
        }

    }

    private void BindData(string strSort)
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add(strSort);
        int RowCount = 0;


        grv.DataSource = TblPhotoRepository.CreateInstant().SelectListItem(null, AlbumId, "", "", null, PageIndex, PageSize, out RowCount, hs);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = RowCount;
        smartPager.UrlFormatString = "/Admin/Module/Photo/PhotoList.aspx?AlbumId=" + AlbumId + "&page={0}";
        smartPager.Visible = RowCount > PageSize;

    }
    protected void grv_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (GridViewSortDirection.Equals("ASC"))
            GridViewSortDirection = "DESC";
        else
            GridViewSortDirection = "ASC";
        string strSort = e.SortExpression + " " + GridViewSortDirection;
        BindData(strSort);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/Module/Photo/PhotoList.aspx?AlbumId=" + AlbumId);
    }
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DeleteItem"))
        {
            HiddenField hdfId = (HiddenField)grv.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("hdfId");
            //string Whereclause = "";
            //string Whereclause1 = " Id='" + hdfId.Value + "'";
            //string Whereclause2 = "And Id<>'" + hdfId.Value + "'";
            try
            {
                TblPhoto obj = TblPhotoRepository.CreateInstant().SelectOne(new Guid(hdfId.Value));
                System.IO.File.Delete(Server.MapPath(obj.ImageUrl));
            }
            catch
            {
                Trace.Write("Loai xoa hinh");
            }
            //UpdateOrder.UpdateOrders("TblAlbum", Whereclause, Whereclause1, Whereclause2, 100);
            TblPhotoRepository.CreateInstant().Delete(new Guid(hdfId.Value));
            Response.Redirect(Request.RawUrl);
        }
    }

}
